package com.codechuan.demo.ui.myToken;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.codechuan.demo.R;
import com.codechuan.demo.adapter.TokenBaseAdapter;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.dto.TokenObject;
import com.codechuan.demo.ui.addMinAccount.AddMinAccountActivity;
import com.codechuan.demo.ui.login.LoginActivity;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.MyHttpUtil;

import java.util.ArrayList;

public class MyToken extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        DialogUtil.showDialog(Constant.context, "获取中,请稍后");
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my_token);
        ListView listView = findViewById(R.id.list_planet);
        if (!Constant.isStart) {
            // 置空公共小号token重新访问数据获取token
            Constant.SON_TOKENS = new ArrayList<>();
            // 置空自己小号token重新访问数据获取token
            Constant.MY_SON_TOKENS = new ArrayList<>();
            // 调用接口获取小号
            Constant.executorService.execute(new Runnable() {
                @Override
                public void run() {
                    MyHttpUtil.getUserTokenList(Constant.USER_ACCOUNT);
                    Constant.handler.post(() -> {
                        // 创建自定义适配器对象查看自己的小号
                        ArrayList<TokenObject> arrayList = new ArrayList<>();
                        for (TokenObject sonToken : Constant.MY_SON_TOKENS) {
                            if (sonToken.getType().equals("1"))
                                arrayList.add(sonToken);
                        }
                        TokenBaseAdapter tokenBaseAdapter = new TokenBaseAdapter(Constant.context, arrayList);
                        listView.setAdapter(tokenBaseAdapter);
                        // 修改小号后重新赋值给小号避免遗漏问题
                        Constant.SON_TOKENS_SHOW = arrayList;
                    });
                }
            });
        } else {
            // 初始化后直接开始抢单会有主号信息需要处理主号,回显不需要置空,就是解决置空线程拿不到token问题
            // SON_TOKENS_SHOW只是回显作用 使用的还是SON_TOKENS
            ArrayList<TokenObject> arrayList = new ArrayList<>();
            for (TokenObject sonToken : Constant.SON_TOKENS_SHOW) {
                if (sonToken.getType().equals("1"))
                    arrayList.add(sonToken);
            }
            TokenBaseAdapter tokenBaseAdapter = new TokenBaseAdapter(this, arrayList);
            listView.setAdapter(tokenBaseAdapter);
        }


        Button jumpHome = findViewById(R.id.jumpHome);
        jumpHome.setOnClickListener(this);

        Button addMinAccount = findViewById(R.id.addMinAccount);
        addMinAccount.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (R.id.jumpHome == id) {
            Intent intent = new Intent();
            intent.setClass(Constant.context, LoginActivity.class);
            startActivity(intent);
        }
        if (R.id.addMinAccount == id) {
            if (!Constant.MY_SON_TOKENS.isEmpty()) {
                DialogUtil.showDialog(Constant.context, "不能添加多个小号");
                return;
            }
            Intent intent = new Intent();
            intent.setClass(Constant.context, AddMinAccountActivity.class);
            startActivity(intent);
        }

    }
}